<?

include '../libraries/support.php';
function print_all_events()
{
	$sql="SELECT * FROM `events` WHERE 1 ORDER BY `events`.`ID` ASC";
	$result = mysql_query($sql) or die (mysql_error());
	for ($i=0;$i<mysql_num_rows($result);$i++)
	{
		$row = mysql_fetch_assoc($result); 
		$ID = $row['ID'];
		if (! empty($row['date']))
				$Date = format_and_extend_date($row['date']);
		else
				$Date = 'Planned';
		$Summary = $row['summary'];
		$Desc = $row['description'];
		$active = $row['active'];
		?>
		<B>Summary:</B><A href="index.php?id=<? echo $ID; ?>&action=edit"> <? echo $Summary; ?></A><BR>
		<B>Description:</B> <? echo $Desc; ?><BR>
		<B>Date:</B> <? echo $Date; ?><BR>
		<? if ($active == 1)
			{
			echo "Event is active. <A href='index.php?action=make_inactive&id=" . $ID . "'>Make inactive</A><BR>";
			}
		else 
			{
			echo "Event is hidden. <A href='index.php?action=make_active&id=" . $ID . "'>Make active</A><BR>";
			}
		if (event_on_main_page($ID))
		{
			echo "Event is shown on home page <A href='index.php?action=home_delete&id=" . $ID . "'> Hide on homepage</A><BR>"; 
		}
		else
		{
			echo "Event is <B>NOT</B> shown on home page <A href='index.php?action=home_add&id=" . $ID . "'>Show on homepage</A><BR>";
		}
		?><BR /><BR />
		<?
	}
	mysql_free_result($result);
}
function make_active($id)
{
$sql="UPDATE `events` SET `active`=1 WHERE `id`= $id ";
mysql_query($sql) or die (mysql_error());
}
function make_inactive($id)
{
$sql="UPDATE `events` SET `active`=0 WHERE `id`= $id ";
mysql_query($sql) or die (mysql_error());
}
function show_event_for_edit($id)
{
	$sql="SELECT * FROM `events` WHERE `id`=$id ";
	$result = mysql_query($sql) or die (mysql_error());
	if (mysql_num_rows($result) < 1)
		die ("No entry with ID=$id");
	?>
	<form name="edit_event" action="index.php?action=save" method="post">
	<table name="Edit event">
	<?
	$event_data=mysql_fetch_assoc($result);
	echo "<tr><td>ID: </td><td>" . $event_data['ID'] . "</td></tr>";
	echo '<input type="hidden" name="id" value="' . $event_data['ID'] . '">';
	echo '<tr><td>Date (day month year): </td><td><input type="text" name="date" value="' . date_to_string($event_data['date']) . '"> </td></tr>';
	echo '<tr><td>Event summary: </td><td><input type="text" name="summary" value="' . $event_data['summary'] . '" > </td></tr>';
	echo '<tr><td>Event description: </td><td><textarea name="description" cols="60" rows="10" >' . $event_data['description'] . '</textarea> </td></tr>';
	echo '<tr><td><input type="submit" value="Save changes"> </td></tr>';
	mysql_free_result($result);
	?>
	</table>
	</form>
	<?
}
function show_event_empty_form()
{
	?>

	<form name="edit_event" action="index.php?action=insert" method="post">
	<table name="Input event">
	<tr><td>Date (day month year): </td><td><input type="text" name="date"> </td></tr>
	<tr><td>Event summary: </td><td><input type="text" name="summary"> </td></tr>
	<tr><td>Event description: </td><td><textarea name="description" cols="60" rows="10"></textarea></td></tr>
	<tr><td><input type="submit" value="Save new event"></td></tr>
	<table>
	</form>
	<?
}
function save_event($id,$date,$summary,$description)
{
	$summary = ucfirst($summary);
	$description = ucfirst($description);
	$sql="UPDATE `events` SET `date`='$date' , `summary` = '$summary' , `description` = '$description' WHERE `ID` = $id";
	mysql_query($sql) or die ("Event update error, $sql");
	if (($date=="0000-00-00") or ($date==""))
	{
		$sql=" UPDATE `events` SET `date`=NULL WHERE `ID`=$id";
		mysql_query($sql) or die("Date cannot be NULL");
	}
}
function insert_event($date,$summary,$description)
{
	$summary = ucfirst($summary);
	$description = ucfirst($description);
	
	if (($date=="0000-00-00") or ($date==""))
	{
		$sql="INSERT INTO `events` (`summary`,`description`) VALUES ('$summary','$description')";
	}
	else
		$sql="INSERT INTO `events` (`date`,`summary`,`description`) VALUES ('$date','$summary','$description')";
	mysql_query($sql) or die ("Event insert error");
}
function print_admin_navigator()
{
	?>
	<div id="navigator">
	<h3>Admin navigation</h3>
	<P><A href="index.php">List events</A></P>
	<P><A href="index.php?action=add_new">Add event</A></P>
	<P><A href="blogadmin.php">Blog administration</A></P>
	<P><A href="gallery_admin.php">Gallery administration</A></P>
	<?//<P><A href="header_edit.php">Headers edition</A></P>?>
	<?//<P><A href="gallery_edit.php">Gallery edition</A></P>?> 
	<P><A href="../auth/logout.php">Log out</A></P>
	</div>
	<?
}
function event_on_main_page($id)
{
	$sql="SELECT * FROM `backbone` WHERE `type_ID`=2 AND `active_item_ID`=$id";
	$result=mysql_query($sql) or die ("Backbone retrival error <BR> $sql");
	if ( mysql_num_rows($result) < 1)
		return 0;
	else
		return 1;
}
function home_add($id)
{
	$order=$id*100;
	$sql="INSERT INTO `backbone` (`active_item_ID`,`type_ID`,`order`) VALUES ('$id',2,$order)";
	mysql_query($sql) or die ("Backbone insertion error: <BR> $sql");
}
function home_delete($id)
{
	$sql="DELETE FROM `backbone` WHERE `active_item_ID`=$id AND `type_ID`=2";
	mysql_query($sql) or die ("Backbone deletion error <BR> $sql");
}
function validate_login($login)
{
	$sql = "SELECT `name` FROM `passwords` WHERE `name` = '$login'";
	$result = mysql_query($sql) or die ("Cannot ask for login");
	if (mysql_num_rows($result) == 0)
		$login_details = 'any';
	else
		$login_details = mysql_result($result,0);
	return $login_details;
}
function get_password($login)
{
	$sql = "SELECT `password` FROM `passwords` WHERE `name` = '$login'";
	$result = mysql_query($sql) or die ("Cannot ask for password");
	if (mysql_num_rows($result)==0)
		$password_details = md5("ljsafksajdfiewjfi092984932jlkdsjflkdsj9i923923jkjkdsjf");
	else
		$password_details = mysql_result($result,0);
	return $password_details;
}
function log_access()
{
	$login = $_SESSION["login"];
	$ip = $_SERVER['REMOTE_ADDR'];
	$date = date("Y-m-d H:i:s", strtotime("+8 hours"));
	$sql = "INSERT INTO `access_trial` (`name`,`date`,`ip`) VALUES('$login', '$date','$ip')";
	mysql_query($sql) or die ("Cannot put access data");
}
function default_list()
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
	<HTML>
	<HEAD>
	<TITLE><? echo "$site_title";?></TITLE>
	<script type="text/javascript"></script>
	
		<style type="text/css" media="all">
			@import "../bbogd.css";
		</style>

	</HEAD>
	<BODY>
	<?
	print_admin_navigator();
	?>
<div id="container">
<div id="main">
	<?
	print_all_events();
	//show_event_for_edit(2);
	//show_event_empty_form();
	
	?>
</div>
</div>
</body>
<?
}
function string_to_date_translator($old_date)
{
	list($inday,$inmonth,$inyear)=explode(" ",$old_date);
	$inmonth = strtoupper($inmonth);
	$inmonth = substr($inmonth,0,3);
	switch($inmonth){
	case "JAN":
		$dmonth="01";
		break;
	case "FEB":
		$dmonth="02";
		break;
	case "MAR":
		$dmonth="03";
		break;
	case "APR":
		$dmonth="04";
		break;
	case "MAY":
		$dmonth="05";
		break;
	case "JUN":
		$dmonth="06";
		break;
	case "JUL":
		$dmonth="07";
		break;
	case "AUG":
		$dmonth="08";
		break;
	case "SEP":
		$dmonth="09";
		break;
	case "OCT":
		$dmonth="10";
		break;
	case "NOV":
		$dmonth="11";
		break;
	case "DEC":
		$dmonth="12";
		break;
	default:
		die("Improper date format");
	break;
	}
	$r_date=$inyear . "-" . $dmonth . "-" . $inday;
	return $r_date;
}
function date_to_string($date)
{
	list($year,$month,$day) = explode("-",$date);
	
	if ($day < 10)
		$day=$day[1];
	
	switch ($month){
	case 1:
		$month = "January";
		break;
	case 2:
		$month = "February";
		break;
	case 3:
		$month = "March";
		break;
	case 4:
		$month = "April";
		break;
	case 5:
		$month = "May";
		break;
	case 6:
		$month = "June";
		break;
	case 7:
		$month = "July";
		break;
	case 8:
		$month = "August";
		break;
	case 9:
		$month = "September";
		break;
	case 10:
		$month = "October";
		break;
	case 11:
		$month = "November";
		break;
	case 12:
		$month = "December";
		break;
	}
	
	$newdate = $day . " " . $month . " " . $year;
	return $newdate;
}
?>